package com.ssmm.ssmm1.service;

import com.ssmm.ssmm1.mapper.UserrMapper;
import com.ssmm.ssmm1.pojo.Userr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.jws.soap.SOAPBinding;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by naojiang on 2017/7/12.
 */
@Service("UserService")
public class UserServiceImpl implements UserService {

    @Autowired
    UserrMapper userrMapper;

    public boolean login(String username, String password) {

        Userr userr = userrMapper.selectByName(username);
        if(userr!=null){
            if(userr.getUsername().equals(username)&&userr.getPassword().equals(password)){
                return true;
            }
            else return false;
        }
        else return false;
    }

    public boolean register(String username, String password, String nickname,
                            String sex,String address) {
        System.out.println(username+"1 "+password+"1 "+nickname+"1 "+sex+"1 "+address);

        Userr userr = userrMapper.selectByName(username);
        if(userr==null){
            userr = new Userr();
            userr.setUsername(username);
            userr.setPassword(password);
            userr.setNickname(nickname);
            userr.setSex(sex);
            userr.setAddress(address);
            userrMapper.insert(userr);
            return true;
        }else{
            return false;
        }
    }

    public boolean delete(String username) {

        System.out.println("delete"+username);
        Userr userr = userrMapper.selectByName(username);
        if(userr!=null){
            userrMapper.deleteByPrimaryKey(userr.getId());
            return true;
        }
        return false;
    }

    public boolean update(String username, String password, String nickname,
                          String sex,String address) {
        Userr userr = userrMapper.selectByName(username);
        if(userr!=null){
            userr.setUsername(username);
            userr.setPassword(password);
            userr.setNickname(nickname);
            userr.setSex(sex);
            userr.setAddress(address);
            userrMapper.updateByPrimaryKey(userr);
            return true;
        }else{
            return false;
        }
    }

    public List<Userr> list() {

        List<Userr> list1 = new ArrayList<Userr>();
        /*int num=27;
        Integer id = new Integer(num);
        Userr userr = userrMapper.selectByPrimaryKey(id);
        while(userr!=null){
            list1.add(userr);
            num++;
            id = new Integer(num);
            userr = userrMapper.selectByPrimaryKey(id);
        }*/
        list1 = userrMapper.selectBy();
        return list1;
    }
}
